/**
  ******************************************************************************
  * @file    ov2640.h
  * @author  mk
  * @brief   Header file of ov2640.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __OV2640_H
#define __OV2640_H

#include <stdint.h>

#define OV2640_MID				0x7FA2
#define OV2640_PID				0x2642

#define OV2640_PWDN_Pin          GPIO_PIN_2
#define OV2640_PWDN_LL_Pin       LL_GPIO_PIN_2
#define OV2640_PWDN_GPIO_Port    GPIOC 
#define OV2640_Pwdn(x)           x?(OV2640_PWDN_GPIO_Port->BSRR = OV2640_PWDN_Pin):(OV2640_PWDN_GPIO_Port->BRR = OV2640_PWDN_Pin)

#define OV2640_RESET_Pin         GPIO_PIN_3
#define OV2640_RESET_LL_Pin      LL_GPIO_PIN_3
#define OV2640_RESET_GPIO_Port   GPIOC 
#define OV2640_Reset(x)          x?(OV2640_RESET_GPIO_Port->BSRR = OV2640_RESET_Pin):(OV2640_RESET_GPIO_Port->BRR = OV2640_RESET_Pin)

typedef enum
{
  BMP_QQVGA    = 0x00,	 /* BMP Image QQVGA 160x120 Size */
  BMP_QVGA     = 0x01,   /* BMP Image QVGA 320x240 Size */
  JPEG_160x120 = 0x02,	 /* JPEG Image 160x120 Size */
  JPEG_176x144 = 0x03,	 /* JPEG Image 176x144 Size */
  JPEG_320x240 = 0x04,	 /* JPEG Image 320x240 Size */
  JPEG_352x288 = 0x05	   /* JPEG Image 352x288 Size */
}ImageFormat_TypeDef;

#define OV2640_DSP_R_BYPASS     0x05u
#define OV2640_DSP_Qs           0x44u
#define OV2640_DSP_CTRL         0x50u
#define OV2640_DSP_HSIZE1       0x51u
#define OV2640_DSP_VSIZE1       0x52u
#define OV2640_DSP_XOFFL        0x53u
#define OV2640_DSP_YOFFL        0x54u
#define OV2640_DSP_VHYX         0x55u
#define OV2640_DSP_DPRP         0x56u
#define OV2640_DSP_TEST         0x57u
#define OV2640_DSP_ZMOW         0x5Au
#define OV2640_DSP_ZMOH         0x5Bu
#define OV2640_DSP_ZMHH         0x5Cu
#define OV2640_DSP_BPADDR       0x7Cu
#define OV2640_DSP_BPDATA       0x7Du
#define OV2640_DSP_CTRL2        0x86u
#define OV2640_DSP_CTRL3        0x87u
#define OV2640_DSP_SIZEL        0x8Cu
#define OV2640_DSP_HSIZE2       0xC0u
#define OV2640_DSP_VSIZE2       0xC1u
#define OV2640_DSP_CTRL0        0xC2u
#define OV2640_DSP_CTRL1        0xC3u
#define OV2640_DSP_R_DVP_SP     0xD3u
#define OV2640_DSP_IMAGE_MODE   0xDAu
#define OV2640_DSP_RESET        0xE0u
#define OV2640_DSP_MS_SP        0xF0u
#define OV2640_DSP_SS_ID        0x7Fu
#define OV2640_DSP_SS_CTRL      0xF8u
#define OV2640_DSP_MC_BIST      0xF9u
#define OV2640_DSP_MC_AL        0xFAu
#define OV2640_DSP_MC_AH        0xFBu
#define OV2640_DSP_MC_D         0xFCu
#define OV2640_DSP_P_STATUS     0xFEu
#define OV2640_DSP_RA_DLMT      0xFFu


#define OV2640_SENSOR_GAIN       0x00u
#define OV2640_SENSOR_COM1       0x03u
#define OV2640_SENSOR_REG04      0x04u
#define OV2640_SENSOR_REG08      0x08u
#define OV2640_SENSOR_COM2       0x09u
#define OV2640_SENSOR_PIDH       0x0Au
#define OV2640_SENSOR_PIDL       0x0Bu
#define OV2640_SENSOR_COM3       0x0Cu
#define OV2640_SENSOR_COM4       0x0Du
#define OV2640_SENSOR_AEC        0x10u
#define OV2640_SENSOR_CLKRC      0x11u
#define OV2640_SENSOR_COM7       0x12u
#define OV2640_SENSOR_COM8       0x13u
#define OV2640_SENSOR_COM9       0x14u
#define OV2640_SENSOR_COM10      0x15u
#define OV2640_SENSOR_HREFST     0x17u
#define OV2640_SENSOR_HREFEND    0x18u
#define OV2640_SENSOR_VSTART     0x19u
#define OV2640_SENSOR_VEND       0x1Au
#define OV2640_SENSOR_MIDH       0x1Cu
#define OV2640_SENSOR_MIDL       0x1Du
#define OV2640_SENSOR_AEW        0x24u
#define OV2640_SENSOR_AEB        0x25u
#define OV2640_SENSOR_W          0x26u
#define OV2640_SENSOR_REG2A      0x2Au
#define OV2640_SENSOR_FRARL      0x2Bu
#define OV2640_SENSOR_ADDVSL     0x2Du
#define OV2640_SENSOR_ADDVHS     0x2Eu
#define OV2640_SENSOR_YAVG       0x2Fu
#define OV2640_SENSOR_REG32      0x32u
#define OV2640_SENSOR_ARCOM2     0x34u
#define OV2640_SENSOR_REG45      0x45u
#define OV2640_SENSOR_FLL        0x46u
#define OV2640_SENSOR_FLH        0x47u
#define OV2640_SENSOR_COM19      0x48u
#define OV2640_SENSOR_ZOOMS      0x49u
#define OV2640_SENSOR_COM22      0x4Bu
#define OV2640_SENSOR_COM25      0x4Eu
#define OV2640_SENSOR_BD50       0x4Fu
#define OV2640_SENSOR_BD60       0x50u
#define OV2640_SENSOR_REG5D      0x5Du
#define OV2640_SENSOR_REG5E      0x5Eu
#define OV2640_SENSOR_REG5F      0x5Fu
#define OV2640_SENSOR_REG60      0x60u
#define OV2640_SENSOR_HISTO_LOW  0x61u
#define OV2640_SENSOR_HISTO_HIGH 0x62u

#define OV2640_CHANGE_REG_NUM       167
#define OV2640_QQVGA_SIZE           328
#define OV2640_QVGA_SIZE            219
#define OV2640_JPEG_INIT_SIZE       190
#define OV2640_YUV422_SIZE          9
#define OV2640_JPEG_SIZE            8
#define OV2640_160x120_JPEG_SIZE    39
#define OV2640_176x144_JPEG_SIZE    39
#define OV2640_320x240_JPEG_SIZE    40
#define OV2640_352x288_JPEG_SIZE    39

extern const uint8_t change_reg[OV2640_CHANGE_REG_NUM][2];
extern const uint8_t OV2640_QQVGA[OV2640_QQVGA_SIZE][2];
extern const uint8_t OV2640_QVGA[OV2640_QVGA_SIZE][2];         /* QVGA 320x240 */
extern const uint8_t OV2640_JPEG_INIT[OV2640_JPEG_INIT_SIZE][2];
extern const uint8_t OV2640_YUV422[OV2640_YUV422_SIZE][2];
extern const uint8_t OV2640_JPEG[OV2640_JPEG_SIZE][2];
extern const uint8_t OV2640_160x120_JPEG[OV2640_160x120_JPEG_SIZE][2]; /* JPG 160x120 */
extern const uint8_t OV2640_176x144_JPEG[OV2640_176x144_JPEG_SIZE][2]; /* JPG 176x144 */
extern const uint8_t OV2640_320x240_JPEG[OV2640_320x240_JPEG_SIZE][2]; /* JPG 320x240 */
extern const uint8_t OV2640_352x288_JPEG[OV2640_352x288_JPEG_SIZE][2]; /* JPG 352x288 */

extern void Dcmi_BspInit(void);
extern void Dcmi_StartCapture(void);
extern void Dcmi_StopCapture(void);
extern void SCCB_BspInit(void);
extern uint8_t SCCB_WriteReg(uint8_t reg, uint8_t data);
extern uint8_t SCCB_ReadReg(uint8_t reg);

extern void OV2640_ChipReset(void);
extern uint8_t OV2640_CheckChipID(void);
extern void OV2640_BrightnessConfig(uint8_t Brightness);
extern void OV2640_JPEGConfig(ImageFormat_TypeDef ImageFormat);
extern void OV2640_JPEG_Mode(void);

#endif /* __OV2640_H */
